<?php
    $item_menu[0] = 6; 
    $item_menu[1] = 1;  
    $title = 'Contratantes';
	
	include('header.php');

	if (!isset($_SESSION["admin"])) {redirect("login.php");	exit(); }

    $titulo         = 'Administracion de Contratantes';    

	$letra  = request('letra','');
	$id     = request('id',0);
	$accion = request('accion','');

	switch ($accion) {

		case 'guardar': /******************************************************************************** GUARDAR LOS CAMBIOS *********************/	
			$data = $_POST['data'];
			$ok = $db->Replace('contratantes', $data,'id', $autoquote = true); 
			$id=0;
			break;
		
		case 'eliminar': /******************************************************************************** DELETE *********************/
			// Elimina la publicacion
			$ok		= $db->Execute("delete from usuarios where id='$id'");
			if ($ok) { echo mensaje_ok("El Usuario se elimin&oacute; correctamente.");
			} else { echo mensaje_error("ERROR!! No se pudo eliminar el Usuario."); echo $sql; }
			$id = 0;
			break;

	}


	$sql="select LEFT(TRIM(apellido),1) as letra from contratantes group by LEFT(apellido,1) order by apellido ASC";
	$rs = $db->Execute($sql);
	$ll = $rs->GetRows();
	sort($ll);
	//pr($ll);
	$Letras = array();
	foreach($ll as $lll){
		$Letras[$lll['letra']] = $lll['letra']; 
		$letra = iif(empty($letra),$lll['letra'],$letra);
	}
	
	if($letra!='todos'){
		$sql="select * from contratantes where LEFT(TRIM(apellido),1)='$letra' order by apellido ASC";
	} else {
		$sql="select * from contratantes  order by apellido ASC";
	}
	$rs = $db->Execute($sql);
	$Usuarios = $rs->GetRows();
	

?>
	<h2><?php echo $titulo;?></h2>
	<a href="reservas-contratantes_editar.php?id=0" role="button" class="btn btn-primary"  style='float:right;'>Nuevo contratante</a>	
	
	<div class="pagination" style='margin:10px;font-size:11px;'>
	  <ul>
	<?php
		echo "<li><a href='reservas-contratantes.php?letra=todos' style='line-height:16px;padding: 3px 5px;' >Todos</a></li>";
		foreach($Letras as $l){
			echo "<li><a href='reservas-contratantes.php?letra={$l[letra]}' style='line-height:16px;padding: 3px 5px;'  >".strtoupper($l[letra])."</a></li>";
            if (empty($letra)) { $letra = $l['letra'];}
		}
		echo "<li><a href='reservas-contratantes_exportar.php' title='Exportar a Excel' style='padding: 3px 5px;' ><img src='".ADMIN."/iconos/xls.gif'></a></li>";
	?>
	  </ul>
	</div>

	<table class="table table-striped table-hover table-condensed" style='font-size:12px;'>
		<thead>
			<tr>
				<th style='text-align:center; width:70px;'>Acción</th>
				<th style='text-align:left; width:200px;'>Apellido y Nombre</th>
				<th style='text-align:left;'>Email</th>
				<th style='text-align:left;'>Telefono</th>
			</tr>
		</thead>
		<tbody>

		<?php foreach($Usuarios as $c) { ?>
			<tr >
				<td align='left' nowrap='nowrap'>
					<a href='reservas-contratantes.php?accion=eliminar&id=<?php echo $c['id'];?>&letra=<?php echo $letra;?>' title='Eliminar este Registro'
						onclick="return confirm('Est&aacute; seguro de eliminar este Registro?');">
						<img src='img/del.gif' border='0' ></a>&nbsp;&nbsp;&nbsp;

					<a href="reservas-contratantes_editar.php?id=<?php echo $c['id'];?>" ><img src='img/edit.gif' border='0' ></a>
						
				</td>
				<td ><?php echo $c['apellido'].", ".$c['nombre'];?>&nbsp;</td>
				<td ><?php echo $c['email'];?></td>
				<td ><?php echo $c['telefono'];?>&nbsp;</td>
			</tr>
		<?php } ?>
		</tbody>

	</table>
